package com.qtyx.nms.ui.adapter

import android.content.Context
import android.graphics.Color
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.ldd.base.ui.adapter.BaseRVAdapter
import com.ldd.base.ui.adapter.BaseRVVH
import com.qtyx.nms.R
import com.qtyx.nms.mvp.model.bean.NurseBean
import com.qtyx.nms.ui.activity.HomePageActivity
import com.qtyx.nms.util.DisplayUtil


/**
 * 护理床位适配器
 */
class NurseBedAdapter(mContext: Context, list: List<NurseBean.Data.SqlData>) :BaseRVAdapter<NurseBean.Data.SqlData>(mContext, list) {
    override fun getLayoutId(viewType: Int)= R.layout.item_nurse_bed

    var selectPosition=-1

    private val margin10=DisplayUtil.dp2px(10f)
    private val margin15=DisplayUtil.dp2px(15f)
    private val margin2=DisplayUtil.dp2px(2f)
    private val margin5=DisplayUtil.dp2px(5f)

    override fun convert(vh: BaseRVVH, position: Int, data: NurseBean.Data.SqlData) {
        val llMain=vh.getView<LinearLayout>(R.id.ll_item_nurse_bed_main)
        val tvBedNum=vh.getView<TextView>(R.id.tv_item_nurse_bed_num)
        val tvName=vh.getView<TextView>(R.id.tv_item_nurse_bed_name)
        val tvAge=vh.getView<TextView>(R.id.tv_item_nurse_bed_age)
        val tvSex=vh.getView<TextView>(R.id.tv_item_nurse_bed_sex)
        //主治医生
        val tvZzys=vh.getView<TextView>(R.id.tv_item_nurse_bed_zzys)
        //责任护士
        val tvZrhs=vh.getView<TextView>(R.id.tv_item_nurse_bed_zrhs)
        //收费种类
        val tvSfzl=vh.getView<TextView>(R.id.tv_item_nurse_bed_sfzl)
        //账户余额
        val tvZhye=vh.getView<TextView>(R.id.tv_item_nurse_bed_zhye)
        val tvGradeText=vh.getView<TextView>(R.id.tv_item_nurse_bed_grade_text)
        val tvGrade=vh.getView<TextView>(R.id.tv_item_nurse_bed_grade)

        if(selectPosition==position){
            llMain.setBackgroundResource(R.drawable.shape_item_nurse_bed_select_bg)
        }else{
            llMain.setBackgroundResource(R.drawable.shape_item_nurse_bed_bg)
        }
        llMain.setOnClickListener{
            selectPosition=position
            notifyDataSetChanged()
        }

        val layoutParams=llMain.layoutParams as ConstraintLayout.LayoutParams
        val tvBedNumParams=tvBedNum.layoutParams as ConstraintLayout.LayoutParams
        //第一排顶距处理
        if(position<6){
            layoutParams.topMargin=0
            tvBedNumParams.topMargin=margin5
        }else{
            layoutParams.topMargin=margin10
            tvBedNumParams.topMargin=margin15
        }

        layoutParams.leftMargin=margin10
        tvBedNumParams.leftMargin=margin2

        tvBedNum.text=data.BED_NO
        tvName.text=data.PATIENT_NAME
        tvAge.text=data.AGE
        tvSex.text=data.SEX
        tvZzys.text=data.DOCTOR_NAME
        tvZrhs.text=data.NURSE_NAME
        tvSfzl.text=data.CHARGE_TYPE
        tvZhye.text="￥${data.BALANCE}"

        val grade=HomePageActivity.gradeMap[data.NURSE_LEVEL.toString()]
        val gradeColor=HomePageActivity.gradeColorMap[data.NURSE_LEVEL.toString()]
        if(!grade.isNullOrEmpty()){
            tvGrade.text=grade
            if(!gradeColor.isNullOrEmpty()){
                tvGradeText.setTextColor(Color.parseColor(gradeColor))
                tvGrade.setTextColor(Color.parseColor(gradeColor))
            }
        }
    }

}